$.include('json')

$(function(){
	cbs[0]();
})
jsessionid = false;
cb = 1;
Jabbify = {
	host: "http://127.0.0.1:8080",
	domain: window.location.hostname ? window.location.hostname.replace(/^www\./,'') : "file",
	jsessionid: false,
	transport: "json"
}
Jabbify.cleanAddress = function(address){
	return address.match(/@/) ? address : Jabbify.domain+"/"+address+"@"+Jabbify.host
}
Jabbify.send = function(data, callback){
	
}
Jabbify.Node = {
	create: function(json, callback){
		
		json = json ? ( typeof json== 'string' ? {data: {address: Jabbify.cleanAddress(json)}} : json ) : {};
		json.type = "node";
		json.action = "create";
		json.to = json.to ? Jabbify.cleanAddress(json.to) : json.to
		if(!json.data) throw "you must provide an address!";
		
	}
}
Jabbify.Node.create
Jabbify.
request = function(data){
		print('\n----------------------------------------------------')
		print(data.type+":"+data.action+"\n ->"+(jsessionid ? "! " : " ")  +$.toJSON(data));
		
		$.get("http://127.0.0.1:8080/message" +(jsessionid ? ";jsessionid="+jsessionid : ""),{
			data: $.toJSON(data),
			format: "json",
			//callback: "response"
		}, function(json){
			print(' <- '+$.toJSON(json))
			cbs[cb++](json)
		} ,'json')
	}

cbs = [
	function(){
		request({
		  type: "node",
		  action: "create",
		  //from: "jmvc.com@jabbify.com", 
		  to: "jmvc.com@jabbify.com",  
		  data : {
		    address: "jmvc.com/justin@jabbify.com",
			nodes: ['#jmvc.com@jabbify.com'], 
		    block: ['hank#jmvc.com@jabbify.com'], 
		    only: ['#jmvc.com@jabbify.com'], 
		    listInfo : {
				name: "Justin",
				website: "http://jabbify.com"
			}
		  }
	    });
	},
	//create a session
	function(json){
		print('uuid '+json.data.uuid);
		user_uuid = json.data.uuid;
		request( {
		  type: "session",
		  action: "create",
		  to: "jmvc.com@jabbify.com", 
		  data : {
		  	 address: "jmvc.com/justin#123@jabbify.com",
			 uuid: json.data.uuid 
		  }
		});
	},
	function(json){
		jsessionid= json.jsessionid;
		map();
	},
	function(json){
		showMap(json);
		request( {
		  type: "session",
		  action: "destroy",
		  to: "jmvc.com/justin@jabbify.com", 
		  data : {
		  	 address: "jmvc.com/justin#123@jabbify.com",
		  	 uuid: user_uuid 
		  }
		} )
	},
	function(json){
		map();
	},
	function(json){
		showMap(json);
		
		request( {
		  type: "node",
		  action: "destroy",
		  to: "jmvc.com/justin@jabbify.com", 
		  data : {
		  	 address: "jmvc.com/justin@jabbify.com",
		  	 uuid: user_uuid 
		  }
		} )
	},
	function(json){
		map();
	},
	function(json){
		showMap(json);
	}
	
]

map = function(){
	request( {
		  type: "domain",
		  action: "map",
		  to: "jmvc.com@jabbify.com", //probably must be present so easy to find
		  data : {}
		});
	mapLevel = 0 ;
}

showMap = function(json){
	if(mapLevel == 0) print( "Mapping....");
	mapLevel++;
	var space = new Array(mapLevel*4).join(' ')
	for(var address in json){
		print(space+address);
		showMap(json[address]);
	}
	mapLevel--;
	if(mapLevel == 0) print( "");
}




